"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 服务端工具
Case Name   : 检查open gaussXML配置文件
Create At   : 2021/9/22
Owner       : opentestcase002
Description : 检查open gaussXML配置文件
Expect      : 检查完成
History     :
    2023-3-22 opentestcase023 优化用例，最新代码取消对磁盘IO检查，修改用例以适配最新代码
    2023-6-13 opentestcase023 优化用例，适配最新代码
"""

import os
import re
import unittest

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
        self.rootNode = Node('default')
        self.Constant = Constant()

    def test_server_tools1(self):
        text = '-----step1:检查open gaussXML配置文件;expect:检查完成-----'
        self.log.info(text)
        check_cmd = f'source {macro.DB_ENV_PATH};' \
                    f'gs_checkos -i A -X {macro.DB_XML_PATH};'
        self.log.info(check_cmd)
        msg = self.rootNode.sh(check_cmd).result()
        self.log.info(msg)
        self.log.info('-----解析执行结果-----')
        info_lis = re.findall('.*\[.*\].*:.*', msg)
        self.log.info(info_lis)
        status_list = []
        num_list = []
        self.log.info('-----提取选项信息-----')
        for i in info_lis:
            num_list.append(i.split('.')[0].strip())
        self.log.info(num_list)
        self.log.info('-----提取状态信息-----')
        for j in info_lis:
            status_list.append(j.split(':')[-1].strip())
        self.log.info(status_list)
        text = '-----断言，判断选项信息是否正确，判断状态信息是否正常-----'
        self.log.info(text)
        check_lis = ['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8',
                     'A9', 'A10', 'A11', 'A12', 'A13', 'A14']
        self.assertEqual(check_lis, num_list, 'checkos检查项失败')
        for check_info in status_list:
            self.assertIn(check_info, ['Normal', 'Warning', 'Abnormal'],
                          '回显信息错误')

    def tearDown(self):
        self.log.info('-----无需清理环境-----')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
